package com.aos.erp.manager.service.mq;

import com.aos.erp.common.exception.BizException;
import com.aos.erp.manager.remote.model.SynUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @program: erp
 * @description:
 * @author: tanzhiyu
 * @create: 2018-11-19 14:48
 **/
@Slf4j
@Component
public class SendMessageServiceImpl implements SendMessageService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private AmqpTemplate template;

    /**
     * 同步用户
     *
     * @param synUser
     * @throws Exception
     */
    public void synUserInfo(Object synUser) throws Exception {
        //设置回调对象
        rabbitTemplate.setConfirmCallback(this);
        //构建回调返回的数据
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(QueueConstants.USER_EXCHANGE_NAME, QueueConstants.USER_QUEUE_ROUTE_SAVE, synUser, correlationData);
        //template.convertAndSend(QueueConstants.USER_EXCHANGE_NAME, QueueConstants.USER_QUEUE_ROUTE_KEY, synUser);
        log.info("保存用户发送: " + synUser);
    }

    /**
     * 同步用户删除
     *
     * @param synUser
     * @throws Exception
     */
    public void deleteUser(Object synUser) throws Exception {
        //设置回调对象
        rabbitTemplate.setConfirmCallback(this);
        //构建回调返回的数据
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(QueueConstants.USER_EXCHANGE_NAME, QueueConstants.USER_QUEUE_ROUTE_DEL, synUser, correlationData);
        log.info("删除用户发送: " + synUser);
    }


    /**
     * 新增用户
     *
     * @param userRole
     * @throws Exception
     */
    public void addUser(Object userRole) throws Exception {
        rabbitTemplate.setConfirmCallback(this);
        //构建回调返回的数据
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(QueueConstants.USER_EXCHANGE_NAME, QueueConstants.USER_ROLE_QUEUE_SAVE, userRole, correlationData);
        log.info("新增用户发送: " + userRole);
    }

    /**
     * 消息回调确认方法
     *
     * @param correlationData 回调数据
     * @param isSendSuccess   是否发送成功
     * @param
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean isSendSuccess, String s) {
        log.info("confirm回调方法>>>>>>>>>>>>>回调消息ID为: " + correlationData.getId());
        if (isSendSuccess) {
            log.info("confirm回调方法>>>>>>>>>>>>>消息发送成功");
        } else {
            log.info("confirm回调方法>>>>>>>>>>>>>消息发送失败" + s);
        }
    }
}

